import axios from 'axios';

/**
 * 从 localStorage 获取用户 ID
 * @returns {string} - 用户 ID
 */
const getUserId = () => {
    const userInfo = localStorage.getItem('user_info');
    if (!userInfo) throw { msg: '用户未登录或未找到用户信息' };

    try {
        const parsedUserInfo = JSON.parse(userInfo);
        return parsedUserInfo.id;
    } catch (error) {
        throw { msg: '用户信息解析失败' };
    }
};

/**
 * 获取用户资产列表
 * @returns {Promise<Object>} - API 响应
 */
export const $getTransaction = async () => {
    try {
        const userId = getUserId(); // 自动获取用户 ID
        const response = await axios.get(`/api/transaction/list?userid=${userId}`);
        console.log(response.data);
        return response.data;
    } catch (error) {
        throw error.response?.data || { msg: 'Api获取交易信息失败' };
    }
};